package com.blue.http;

import com.blue.http.config.HttpConfig;
import com.blue.http.config.SSLConfig;
import com.blue.http.config.WebsocketConfig;
import com.blue.http.server.HttpInitializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.task.TaskExecutor;

/**
 * Netty实现接口器
 * 
 * @author zhengj
 * @since 1.0 2017年3月1日
 */
public class NettyHttpServer implements HttpServer, InitializingBean
{
	private static Logger logger = LoggerFactory.getLogger(NettyHttpServer.class);

	private int ioThread = Runtime.getRuntime().availableProcessors();
	private int port;
	private TaskExecutor taskExecutor;
	private HttpConfig httpConfig;
	private WebsocketConfig websocketConfig;
	private SSLConfig sslConfig;

	private HttpInitializer initializer;
	
	public NettyHttpServer()
	{
	}

	@Override
	public void start() throws Exception
	{
		this.start(-1);
	}

	@Override
	public void start(long start) throws Exception
	{
		EventLoopGroup bossGroup = new NioEventLoopGroup(1);
		EventLoopGroup workerGroup = new NioEventLoopGroup(ioThread);

		try
		{
			ServerBootstrap bootstrap = new ServerBootstrap();
			bootstrap.group(bossGroup, workerGroup)
					.channel(NioServerSocketChannel.class)
					.childHandler(initializer)
					.option(ChannelOption.SO_BACKLOG, 1024);

			ChannelFuture channelFuture= bootstrap.bind(port).sync();
			if (start > 0)
			{
				logger.info("服务器启动成功，监听 {} 端口，用时 {} ms。。。", port, System.currentTimeMillis() - start);
			}
			else
			{
				logger.info("服务器启动成功，监听 {} 端口。。。", port);
			}
			channelFuture.channel().closeFuture().sync();
			logger.info("服务器关闭");
		}
		finally
		{
			workerGroup.shutdownGracefully();
			bossGroup.shutdownGracefully();
		}
	}

	@Override
	public void afterPropertiesSet() throws Exception
	{
		initializer = new HttpInitializer();
		initializer.setTaskExecutor(taskExecutor);
		initializer.setHttpConfig(httpConfig);
		initializer.setWebsocketConfig(websocketConfig);
		initializer.setSslConfig(sslConfig);
		initializer.afterPropertiesSet();
	}

	public void setIoThread(int ioThread)
	{
		this.ioThread = ioThread;
	}

	public void setPort(int port)
	{
		this.port = port;
	}

	public void setTaskExecutor(TaskExecutor taskExecutor)
	{
		this.taskExecutor = taskExecutor;
	}

	public void setHttpConfig(HttpConfig httpConfig)
	{
		this.httpConfig = httpConfig;
	}

	public void setWebsocketConfig(WebsocketConfig websocketConfig)
	{
		this.websocketConfig = websocketConfig;
	}

	public void setSslConfig(SSLConfig sslConfig)
	{
		this.sslConfig = sslConfig;
	}

	public int getIoThread()
	{
		return ioThread;
	}

	public int getPort()
	{
		return port;
	}
}
